package cn.aran.pet.service.impl;

import cn.aran.basic.utils.BaiduAuditUtils;
import cn.aran.pet.domain.Pet;
import cn.aran.pet.domain.PetDetail;
import cn.aran.pet.domain.PetOnlineAuditLog;
import cn.aran.pet.mapper.PetDetailMapper;
import cn.aran.pet.mapper.PetMapper;
import cn.aran.pet.mapper.PetOnlineAuditLogMapper;
import cn.aran.pet.service.IPetService;
import cn.aran.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aran
 * @since 2022-07-16
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    /**
    * @Title: save
    * @Description: 新增宠物
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/16 21:40
    * @Parameters: [pet]
    * @Return void
    */
    @Override
    public void save(Pet pet) {
        petMapper.save(pet);
        PetDetail detail = pet.getDetail();
        detail.setPetId(pet.getId());
        petDetailMapper.save(detail);
    }

    /**
    * @Title: update
    * @Description: 修改
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/16 21:41
    * @Parameters: [pet]
    * @Return void
    */
    @Override
    public void update(Pet pet) {
        petMapper.update(pet);
        petDetailMapper.update(pet.getDetail());
    }

    /**
    * @Title: remove
    * @Description: 删除
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/16 21:41
    * @Parameters: [id]
    * @Return void
    */
    @Override
    public void remove(Long id) {
        petMapper.remove(id);
        petDetailMapper.removeByPetId(id);
    }

    /**
    * @Title: onsale
    * @Description: 批量上架
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/17 0:30
    * @Parameters: [ids]
    * @Return void
    */
    @Override
    public void onsale(List<Long> ids) {
        List<Long> listIds = new ArrayList<>();
        for (Long id : ids) {
            Pet pet = petMapper.loadById(id);
            StringBuffer sb = new StringBuffer();
            // 2.调用百度AI进行文字审核
            if (!BaiduAuditUtils.TextCensor(pet.getName())) {
                sb.append("宠物名称不合法，宠物名称为：" + pet.getName());
            }else {
                listIds.add(id);
            }
            if (sb.length() > 0) {
                // 违规
                PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                petOnlineAuditLog.setPetId(id);
                petOnlineAuditLog.setNote(sb.toString());
                petOnlineAuditLogMapper.save(petOnlineAuditLog);
                continue;
            }
        }
        petMapper.onsale(listIds);
    }

    /**
    * @Title: offsale
    * @Description: 批量下架
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/17 0:30
    * @Parameters: [ids]
    * @Return void
    */
    @Override
    public void offsale(List<Long> ids) {
        petMapper.offsale(ids);
    }

    /**
    * @Title: getDetail
    * @Description: 获取详情
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/17 1:02
    * @Parameters: [id]
    * @Return void
     * @return
    */
    @Override
    public PetDetail getDetail(Long id) {
        return petDetailMapper.loadByPetId(id);
    }
}
